package com.baidu.health.service;

import com.baidu.health.pojo.TbGoodsVo;
import com.baidu.health.pojo.TbOrderItem;
import com.baidu.health.util.ResultResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baidu.health.pojo.TbGoods;
import com.baidu.health.service.TbGoodsService;
import com.baidu.health.mapper.TbGoodsMapper;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 *
 */
@Service
public class TbGoodsServiceImpl extends ServiceImpl<TbGoodsMapper, TbGoods>
    implements TbGoodsService{
    @Resource
    private TbGoodsMapper mapper;
    @Autowired
    private ElasticsearchRestTemplate es;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public ResultResponse fulladd(TbGoods tbGoods) {
        List<TbGoods> tbGoods1= mapper.selectList(null);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        tbGoods.setGoodsUpdatetime(format);
        mapper.insert(tbGoods);
        es.save(tbGoods1);
        return ResultResponse.SUCCESS("同步成功");
    }

    @Override
    @Scheduled(cron = "0/5 * * * * *")
    public ResultResponse add() {
        String s = stringRedisTemplate.opsForValue().get("goodsUpdatetime");
        List<TbGoods> tbGoodsss = new ArrayList<>();
        if (s!=null){
            tbGoodsss=mapper.selectupdate(s);
        }else{
            tbGoodsss=mapper.findlist();
        }
        System.out.println("需要更新"+tbGoodsss.size()+"次");
        HashMap<String, Object> map = new HashMap<>();
        for (TbGoods goodsss : tbGoodsss) {
            es.save(goodsss);
            map.put("goodsss",goodsss);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        stringRedisTemplate.opsForValue().set("goodsUpdatetime",format);
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse goodslist(TbGoodsVo tbGoodsVo) {
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        if (!StringUtils.isEmpty(tbGoodsVo.getGoodsName())){
            boolQueryBuilder.filter(QueryBuilders.multiMatchQuery(tbGoodsVo.getGoodsName(),"goodsName"));
        }
        searchSourceBuilder.query(boolQueryBuilder);
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<font color=\"red\">").postTags("</font>").field("goodsName");
        searchSourceBuilder.highlighter(highlightBuilder);
        builder.withHighlightBuilder(highlightBuilder);
        builder.withPageable(PageRequest.of(tbGoodsVo.getPage()-1,tbGoodsVo.getSize()));
        builder.withQuery(boolQueryBuilder);
        SearchHits<TbGoods> search = es.search(builder.build(), TbGoods.class);
        List<TbGoods> goodslist = new ArrayList<>();
        long totalHits = search.getTotalHits();
        for (SearchHit<TbGoods> tbGoodsSearchHit : search) {
            String ss="";
            List<String> goodsName = tbGoodsSearchHit.getHighlightFields().get("goodsName");
            TbGoods content = tbGoodsSearchHit.getContent();
            if (goodsName!=null){
                for (String s : goodsName) {
                    if (s!=null){
                        ss+=s;
                    }
                }
                content.setGoodsName(ss);
            }
            goodslist.add(content);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("goodslist",goodslist);
        map.put("total",totalHits);
        return ResultResponse.SUCCESS(map);
    }

    @Override
    public ResultResponse goodsorderItem() {

        List<TbOrderItem> list1= mapper.goodsorderItem();
        return ResultResponse.SUCCESS(list1);
    }
}




